package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.hyt.core.base.BaseEntity;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.service.gov.mapper.TimeSubjectMapper;
import com.hyt.it.ogt.kq.service.gov.model.dto.arrange.TimeSubjectCandidateDTO;
import com.hyt.it.ogt.kq.service.gov.model.dto.arrange.TimeSubjectDTO;
import com.hyt.it.ogt.kq.service.gov.model.entity.TimeSubject;
import com.hyt.it.ogt.kq.service.gov.roomdata.model.gce.GceSubject;
import com.hyt.it.ogt.kq.service.gov.roomdata.model.obt.ObtSubject;
import com.hyt.it.ogt.kq.service.gov.service.ISubjectService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeSubjectService;
import com.hyt.it.ogt.kq.service.model.entity.Subject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 批次科目表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2021-03-08
 */
@Service
public class TimeSubjectServiceImpl extends BaseServiceImpl<TimeSubjectMapper, TimeSubject> implements ITimeSubjectService {

    @Resource
    private ISubjectService iSubjectService;

    @Override
    public boolean isTimeSubjectBinding(String taskId, String subjectId) {
        List<TimeSubject> timeSubjects = this.baseMapper.selectTimeSubjectBySubjectId(taskId, subjectId);
        return !CollectionUtils.isEmpty(timeSubjects);
    }

    /**
     * 查询科目编号是否存在
     * @param taskId
     * @param
     * @return
     */
    @Override
    public boolean isCodeIsExists(String taskId, Subject subject){
        Subject byId = iSubjectService.getById(subject.getId());
        if(Objects.isNull(byId)){
            //暂无科目数据
            throw new KqException(ResponseCode.ARRANGE_SUBJECT_NOT_FOUND.getCode(),ResponseCode.ARRANGE_SUBJECT_NOT_FOUND.getMsg());
        }
        //判断code是否重复
        LambdaQueryWrapper<Subject> query = Wrappers.lambdaQuery();
        query.ne(Subject::getId,subject.getId()).eq(Subject::getCode,subject.getCode()).eq(Subject::getDelFlag,Boolean.FALSE);
        query.eq(Subject::getTaskId,taskId);
        int count = iSubjectService.count(query);
        if(count > 0){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public String haveTimeSubjectBinding(String taskId, String subjectIds) {
        List<TimeSubject> timeSubjects = this.baseMapper.selectTimeSubjectBySubjectIds(taskId, subjectIds);
        return timeSubjects.stream().map(subject -> subject.getSubjectId()).collect(Collectors.joining(","));
    }

    @Override
    public List<TimeSubject> listByTaskId(String taskId) {
        QueryWrapper<TimeSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TimeSubject::getTaskId, taskId)
                .eq(TimeSubject::getDelFlag, false);
        return list(queryWrapper);
    }

    @Override
    public List<ObtSubject> getTimeSubjectList(String taskId, String timeId) {
        return baseMapper.getTimeSubjectList(taskId, timeId);
    }

    @Override
    public List<GceSubject> getGceSubjectList(String taskId, String timeId) {
        return baseMapper.getGceSubjectList(taskId, timeId);
    }

    @Override
    public List<String> selectSubjectIds(String taskId) {
        return baseMapper.selectSubjectIds(taskId);
    }

    @Override
    public Integer countTimeSubject(String taskId) {
        return this.lambdaQuery()
                .eq(TimeSubject::getTaskId, taskId)
                .eq(TimeSubject::getDelFlag, false)
                .count();
    }

    @Override
    public Map<String,String> getTimeSubjectMap(String taskId) {
        //科次信息--批次科目
        List<TimeSubject> timeSubjectList = this.lambdaQuery()
                .eq(TimeSubject::getTaskId,taskId)
                .isNotNull(TimeSubject::getSubjectId)
                .eq(TimeSubject::getDelFlag,false)
                .select(TimeSubject::getTimeId,TimeSubject::getSubjectId)
                .list();

        Map<String,String> timeSubjectMap = Maps.newHashMap();
        timeSubjectList.stream().forEach(timeSubject -> {
            String timeSubjectKey = StringUtils.join(timeSubject.getTimeId(),"_",timeSubject.getSubjectId());
            timeSubjectMap.put(timeSubjectKey,"0");
        });

        return timeSubjectMap;
    }

    @Override
    public List<TimeSubjectCandidateDTO> getTimeSubjectCandidateDTO(String taskId) {
        return baseMapper.getTimeSubjectCandidateDTO(taskId);
    }

    @Override
    public Boolean existTimeSubject(String taskId, String subject,String timeId) {
        Integer count = lambdaQuery()
                .eq(TimeSubject::getTaskId,taskId)
                .eq(TimeSubject::getSubjectId,subject)
                .notIn(StringUtils.isNotBlank(timeId),TimeSubject::getTimeId,timeId)
                .eq(TimeSubject::getDelFlag,false)
                .count();

        if (count > 0) {
            //科目已经绑定批次
            return true;
        }
        return false;
    }

    @Override
    public boolean hasTimeSubject(String taskId, String timeId, String subjectId) {
        return this.lambdaQuery().select(TimeSubject::getId)
                .eq(TimeSubject::getTaskId, taskId)
                .eq(TimeSubject::getTimeId, timeId)
                .eq(TimeSubject::getSubjectId, subjectId)
                .eq(TimeSubject::getDelFlag, false)
                .last("LIMIT 1")
                .oneOpt()
                .isPresent();
    }

    @Override
    public List<TimeSubjectDTO> getTimeSubjectDTO(String taskId) {
        return baseMapper.getTimeSubjectDTO(taskId);
    }

    @Override
    public Map<String, String> getSubjectMap(String taskId) {
        Map<String, String> map = new HashMap<>();
        List<Subject> list = iSubjectService.lambdaQuery().eq(Subject::getTaskId, taskId).eq(BaseEntity::getDelFlag, false).list();
        for (Subject subject : list) {
            map.put(subject.getName(), subject.getId());
        }
        return map;
    }

    @Override
    public int countByTime(String taskId, String timeId) {
        return baseMapper.countByTime(taskId, timeId);
    }

    @Override
    public int countSubjectByTime(String taskId) {
        return baseMapper.countSubjectByTime(taskId);
    }
}
